const util = require('../../../util/util.js')
const app = getApp();

const formatLocation = util.formatLocation

Page({
  onShareAppMessage() {
    return {
      title: '使用原生地图选择位置',
      path: 'page/API/pages/choose-location/choose-location'
    }
  },
  data: {
    hasLocation: false,
  },
  onLoad: function (local){
    const that = this
    wx.chooseLocation({
      success(res) {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        if (local.type) {
          prevPage.setData({
            type: local.type,
            localhost: res
          });
        } else {
          prevPage.setData({
            localhost: res
          });
        }
        that.setData({
          hasLocation: true,
          location: formatLocation(res.longitude, res.latitude),
          locationAddress: res.address
        })
        wx.navigateBack({
          delta: 1
        })
      },
      cancel: function(){
        wx.navigateBack({
          delta: 1
        })
      }
    })
  }
})
